package com.gss.armory.domain;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.OneToMany;
import javax.persistence.Table;
import javax.persistence.Transient;
import javax.validation.constraints.Pattern;

import org.hibernate.validator.constraints.Length;
import org.hibernate.validator.constraints.NotBlank;
import org.springframework.format.annotation.DateTimeFormat;

import com.gss.common.domain.CommonDetailsDTO;
import com.gss.common.util.ApplicationConstants;
import com.gss.common.util.ApplicationMaps;


@Entity
@Table(name="DOC_GENERAL_DETAILS")
public class DocGeneralDetailsDTO extends CommonDetailsDTO implements IDocument, Serializable{
	
	public enum DocumentFlowStatus {
		Active,Deactive,Expired,Deleted,Wfip,Due,Overdue,RenewWfip,Versioned,Renewed
	};
	private DocumentFlowStatus documentStatus;
	private long docId;
	@Pattern(regexp=ApplicationConstants.REGEX_FOR_DOC_NAME,message="Invalid Document Name")
	@Length(max=1000)
	private String docName;
	@Pattern(regexp=ApplicationConstants.REGEX_FOR_DESC,message="Invalid Document Desc")
	private String docDesc;
	private int docCategoryId;
	private int docSubCategoryId;
	private int docTypeId;
	@NotBlank
	private String docOwnerId;
	@NotBlank
	private String docOwnerName;
	private List<DocumentMetadataValueDTO> metadataValueList = new ArrayList<DocumentMetadataValueDTO>();
	private String docCode;
	// doc validity details
	private long docLocId;
	private long docDepttId;
	private boolean renewable;
	
	// doc validity details
	@DateTimeFormat(pattern="dd-MM-yyyy")
	private Date docValidityStartDate;
	@DateTimeFormat(pattern="dd-MM-yyyy")
	private Date docValidityEndDate;
	private int firstEscalationPeriod;
	private int secondEscalationPeriod;
	private int thirdEscalationPeriod;
	
	private String docType;
	private String docCategory;
	private String docSubCategory;
	private String docLocation;
	private String docDepartment;
	
	@Pattern(regexp=ApplicationConstants.REGEX_FOR_DESC)
	private String docIssuingAuthority;
	@Pattern(regexp=ApplicationConstants.REGEX_FOR_DESC)
	private String docEnteredWith;
	
	private Integer docCompanyId;
	private String companyName;
	private int overDueByDays;
	
	
	public void setDocSubCategory(String docSubCategory) {
		this.docSubCategory = docSubCategory;
	}
	@Id
	@GeneratedValue(strategy=GenerationType.AUTO)
	@Column(name="DGD_ID")
	public long getDocId() {
		return docId;
	}
	@Column(name="DGD_DOC_NAME")
	public String getDocName() {
		return docName;
	}
	@Column(name="DGD_DOC_DESC")
	public String getDocDesc() {
		return docDesc;
	}
	@Column(name="DGD_CATEGORY_ID")
	public int getDocCategoryId() {
		return docCategoryId;
	}
	@Column(name="DGD_SUBCATEGORY_ID")
	public int getDocSubCategoryId() {
		return docSubCategoryId;
	}
	@Column(name="DGD_DOC_TYPE_ID")
	public int getDocTypeId() {
		return docTypeId;
	}
	@Column(name="DGD_DOC_OWNER_ID")
	public String getDocOwnerId() {
		return docOwnerId;
	}
	@Column(name="DGD_DOC_OWNER_NAME")
	public String getDocOwnerName() {
		return docOwnerName;
	}
	@Column(name="DGD_DOC_LOC_ID")
	public long getDocLocId() {
		return docLocId;
	}
	@Column(name="DGD_DOC_DEPTT_ID")
	public long getDocDepttId() {
		return docDepttId;
	}
	@Column(name="DGD_ISRENEWABLE")
	public boolean isRenewable() {
		return renewable;
	}
	@Column(name="DGD_DOC_VALIDITY_START_DATE")
	public Date getDocValidityStartDate() {
		return docValidityStartDate;
	}
	@Column(name="DGD_DOC_VALIDITY_END_DATE")
	public Date getDocValidityEndDate() {
		return docValidityEndDate;
	}
	@Column(name="DGD_FIRST_ESCALATION_PERIOD")
	public int getFirstEscalationPeriod() {
		return firstEscalationPeriod;
	}
	@Column(name="DGD_SECOND_ESCALATION_PERIOD")
	public int getSecondEscalationPeriod() {
		return secondEscalationPeriod;
	}
	@Column(name="DGD_THIRD_ESCALATION_PERIOD")
	public int getThirdEscalationPeriod() {
		return thirdEscalationPeriod;
	}
	@Column(name="DGD_DOC_STATUS",length=15)
	@Enumerated(value=EnumType.STRING)
	public DocumentFlowStatus getDocumentStatus() {
		return documentStatus;
	}
	@Column(name="DGD_DOCCODE")
	public String getDocCode() {
		return docCode;
	}
	@Column(name="DGD_ISSUING_AUTH",length=101)
	public String getDocIssuingAuthority() {
		return docIssuingAuthority;
	}
	@Column(name="DGD_ENTERED_WITH",length=501)
	public String getDocEnteredWith() {
		return docEnteredWith;
	}
	@Column(name="DOC_COMPANY_ID",nullable=false)
	public Integer getDocCompanyId() {
		return docCompanyId;
	}
	public void setDocId(long docId) {
		this.docId = docId;
	}
	public void setDocName(String docName) {
		this.docName = docName;
	}
	public void setDocDesc(String docDesc) {
		this.docDesc = docDesc;
	}
	public void setDocCategoryId(int docCategoryId) {
		this.docCategoryId = docCategoryId;
	}
	public void setDocSubCategoryId(int docSubCategoryId) {
		this.docSubCategoryId = docSubCategoryId;
	}
	public void setDocTypeId(int docTypeId) {
		this.docTypeId = docTypeId;
	}
	public void setDocOwnerId(String docOwnerId) {
		this.docOwnerId = docOwnerId;
	}
	public void setDocOwnerName(String docOwnerName) {
		this.docOwnerName = docOwnerName;
	}
	public void setDocLocId(long docLocId) {
		this.docLocId = docLocId;
	}
	public void setDocDepttId(long docDepttId) {
		this.docDepttId = docDepttId;
	}
	public void setRenewable(boolean renewable) {
		this.renewable = renewable;
	}
	public void setDocValidityStartDate(Date docValidityStartDate) {
		this.docValidityStartDate = docValidityStartDate;
	}
	public void setDocValidityEndDate(Date docValidityEndDate) {
		this.docValidityEndDate = docValidityEndDate;
	}
	public void setFirstEscalationPeriod(int firstEscalationPeriod) {
		this.firstEscalationPeriod = firstEscalationPeriod;
	}
	public void setSecondEscalationPeriod(int secondEscalationPeriod) {
		this.secondEscalationPeriod = secondEscalationPeriod;
	}
	public void setThirdEscalationPeriod(int thirdEscalationPeriod) {
		this.thirdEscalationPeriod = thirdEscalationPeriod;
	}
	public void setDocumentStatus(DocumentFlowStatus documentStatus) {
		this.documentStatus = documentStatus;
	}

	public void setDocCode(String docCode) {
		this.docCode = docCode;
	}
	@OneToMany(targetEntity=DocumentMetadataValueDTO.class,fetch=FetchType.LAZY,mappedBy="docId")
	public List<DocumentMetadataValueDTO> getMetadataValueList() {
		return metadataValueList;
	}
	public void setMetadataValueList(List<DocumentMetadataValueDTO> metadataValueList) {
		this.metadataValueList = metadataValueList;
	}
	
	
	@Transient
	public String getDocLocation() {
		if(0 != docLocId)
			return ApplicationMaps.LOCID_LOCNAME_MAP.get(docLocId);
		else
			return "";
	}
	public void setDocLocation(String docLocation) {
		this.docLocation = docLocation;
	}
	@Transient
	public String getCompanyName() {
		if(0 != docCompanyId)
			return ApplicationMaps.COMPANYID_COMPANY_MAP.get(docCompanyId).getCdName();
		else
			return "";
	}
	public void setCompanyName(String companyName) {
		this.companyName = companyName;
	}
	@Transient
	public String getDocDepartment() {
		if(0 != docDepttId)
			return ApplicationMaps.DEPTID_DEPTSNAME_MAP.get(docDepttId);
		else
			return "";
	}
	public void setDocDepartment(String docDepartment) {
		this.docDepartment = docDepartment;
	}
	@Transient
	public String getDocType() {
		return ApplicationMaps.DOCUMENT_TYPE_ID_NAME_MAP.get(docTypeId);
	}
	public void setDocType(String docType) {
		this.docType = docType;
	}
	
	@Transient
	public String getDocCategory() {
		if(0 != docCategoryId)
			return ApplicationMaps.DOCUMENT_CAT_ID_MAP.get(docCategoryId);
		else
			return "";
	}
	public void setDocCategory(String docCategory) {
		this.docCategory = docCategory;
	}
	
	@Transient
	public String getDocSubCategory() {
		if(0 != docSubCategoryId)
			return ApplicationMaps.DOCUMENT_SUB_CAT_ID_MAP.get(docSubCategoryId);
		else
			return "";
	}
	public void setDocIssuingAuthority(String docIssuingAuthority) {
		this.docIssuingAuthority = docIssuingAuthority;
	}
	public void setDocEnteredWith(String docEnteredWith) {
		this.docEnteredWith = docEnteredWith;
	}
	
	public void setDocCompanyId(Integer docCompanyId) {
		this.docCompanyId = docCompanyId;
	}
	
	@Transient
	public int getOverDueByDays() {
		overDueByDays = 0;
		if(isRenewable() && null != getDocValidityEndDate()){
			if(getDocValidityEndDate().before(new Date())){
				overDueByDays = (int) (((new Date()).getTime() - getDocValidityEndDate().getTime()) / (1000 * 60 * 60 * 24));
			}
		}
		
		return overDueByDays;
	}
	public void setOverDueByDays(int overDueByDays) {
		this.overDueByDays = overDueByDays;
	}
	
	@Override
	public int hashCode() {
		// TODO Auto-generated method stub
		return super.hashCode();
	}
	
	@Override
	public boolean equals(Object obj) {
		boolean equal = false;
		// TODO Auto-generated method stub
		if(obj instanceof DocGeneralDetailsDTO){
			equal = this.getDocId() == ((DocGeneralDetailsDTO)obj).getDocId() ? true : false;
		}
		return equal;
	}
}
